/* global window */
import modelExtend from "dva-model-extend";
import { config } from "utils";
import {
  query,
  getApplication,
  applicationBorrow,
  getBorrowUpdate,
  borrowAgain1,
  borrowHistory1,
  borrowHistoryDetails1,
  workflowHistory1
} from "services/borrow";
import { pageModel } from "./common";
import { message } from "antd";
import { infoDetais } from "services/search";

const { prefix } = config;
const permissions = JSON.parse(sessionStorage.getItem("permissions"))

export default modelExtend(pageModel, {
  namespace: "borrow",
  state: {
    modalVisibleApp: false, //借阅申请弹窗
    operatingState: 0, //查询状态 0-申请 1-我的 2-待审核 3-已审核
    operatingType: 0, //查询类型 0-借阅  1-归还
    currentItem: {},
    modalVisibleChecking: false, //待审核弹窗
    modalVisibleReturn: false, //档案归还弹窗
    showDetails: false, //详情页面
    info: "", //详情内容
    showBorrowModal:false, //续借模板
    showBorrowCheckModal:false, //续借审核模板
    goList:false,  //历史列表
    goDetails:false, //详情内容
    borrowHistoryList:"", //借阅历史详情
    list1:"",  //借阅历史的列表
    list2:"",  //借阅历史的详情 
    successTotal:"",  //被成功借阅的次数
    menu: permissions.indexOf("borrowManager:borrow:apply") != -1 ? "0" : "2",  //判断权限如果是有申请权限则key为0，无则key为1
    menus: permissions.indexOf("borrowManager:return:apply") != -1 ? "0" : "2",  //判断权限如果是有申请权限则key为0，无则key为1
    pagination1: {
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `共 ${total} 条`,
      current: 1,
      total: null
    },
    pagination2: {
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `共 ${total} 条`,
      current: 1,
      total: null
    },
    sliderData:"", //侧边栏详情
    workflowList:"",
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (
          location.pathname === "/borrowManager/borrow" ||
          location.pathname === "/borrowManager/return"
        ) {
          // dispatch({
          //   type: "query",
          //   payload: { page: 1, pageSize: 10, operatingState: 0 }
          // });
          dispatch({
            type: "fistCom",
          });
        }
      });
    }
  },

  effects: {
    *query({ payload = {} }, { call, put, select }) {
      const operatingState = yield select(
        ({ borrow }) => borrow.operatingState
      );
      let operatingType = 0;
      let url = window.location.href;
      if (url.indexOf("/borrowManager/return") > -1) {
        operatingType = 1;
      }
      const params = {
        operatingState,
        operatingType
      };
      if(params.operatingState == undefined){
        params.operatingState = 0
      }
      const data = yield call(query, { ...params, ...payload });
      if (data) {
        yield put({
          type: "querySuccess",
          payload: {
            list: data.page.list,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: data.page.totalCount
            }
          }
        });
      }
    },
    //申请借阅详情
    *getDetail({ payload }, { call, put, select }) {
      const param = {
        id: payload.borrowId
      };
      const data = yield call(getApplication, param);
      if (data.code === 0) {
        const record = { ...payload, ...data.borrowReturn };
        yield put({
          type: "showCheckingModal",
          payload: record
        });
      }
    },
    *getSliderDetail({ payload }, { call, put, select }) {
      const param = {
        id: payload.borrowId
      };
      const data = yield call(getApplication, param);
      if (data.code === 0) {
        const record = { ...payload, ...data.borrowReturn };
        yield put({
          type: "getSliderDetailSuccess",
          payload: record
        });
      }
    },
    *showDetails({ payload }, { call, put }) {
      const data = yield call(infoDetais, { id: payload });
      yield put({
        type: "showDetailsSuccess",
        payload: {
          showDetails: true,
          info: data
        }
      });
    },
    *showDetailsinfo({payload},{call,put}){
    		const data = yield call(infoDetais,{id:payload.id,borrowId:payload.borrowId})
    		yield put({
    			type:'showDetailsSuccess',
    			payload:{
    				showDetails:true,
    				info:data
    			}
    		})
    },
    *showList({ payload }, { call, put }) {
      yield put({
        type: "hidePageSuccess",
        payload: {
          showDetails: false
        }
      });
    },
    //申请借阅
    *saveApplication({ payload }, { call, put, select }) {
      const data = yield call(applicationBorrow, payload);
      if (data.code === 0) {
        yield put({
          type: "query",
          payload: { page: 1, pageSize: 10 }
        });
      }
    },
    //借阅申请、归还申请、归还审批
    *updateApplication({ payload }, { call, put, select }) {
      const data = yield call(getBorrowUpdate, payload);
      const menu = yield select(
        ({ borrow }) => borrow.menu
      );
      if (data.code === 0) {
        yield put({
          type: "query",
          payload: { operatingState:menu, page: 1, pageSize: 10 }
        });
      }
    },
    
    //详情中的审批
    *detailsUpdate({payload},{call,put}){
    	  const data = yield call(getBorrowUpdate,payload);
    	  if(data.success){
    	  	yield put({
          type: "query",
          payload: { page: 1, pageSize: 10 }
        });
    	  	yield put({
	        type: "hidePageSuccess",
	        payload: {
	          showDetails: false
	        }
      	});
    	  }
    },
    //获取审批流程
    *getworkflowHistory({payload},{call,put}){
      const data = yield call(workflowHistory1,payload);
      if(data.success){
        yield put({
          type:"workflowSuccess",
          payload:{
            workflowList:data.applyList
          }
        })
      }
    },

    //续借申请
  *borrowAgainApply({payload},{call,put}){
      const data =  yield call(borrowAgain1,payload)
      if(data){
        yield put ({type:"hideBorrowAgain"})
        yield put({
          type: "query",
          payload: { page: 1, pageSize: 10 }
        });
      }
    },
    *borrowAgainCheck({payload},{call,put}){
      const data =  yield call(borrowAgain1,payload)
      if(data){
        yield put ({type:"hideAgainModal"})
        yield put({
          type: "query",
          payload: { page: 1, pageSize: 10 }
        });
      }
    },
    *borrowHistory({payload},{call,put}){
      const data = yield call(borrowHistory1,payload)
      if(data){
        yield put ({
          type:"querySuccess",
          payload: {
            list: data.page.list,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: data.page.totalCount
            }
          }
        })
      }
    },
    *borrowHistoryDetails1({payload},{call,put}){
      const data = yield call(borrowHistoryDetails1,payload)
      if(data){
        yield put ({
          type:"querySuccess",
          payload: {
            list: data.page.list,
            successTotal:data.successTotal,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: data.page.totalCount
            }
          }
        })
        yield put ({
          type:"querySuccess1",
          payload: {
            successTotal:data.successTotal,
          }
        })
      }
    },
    *showMenu({ payload }, { call, put }) {
			yield put({
				type: 'showMenuSuccess',
				payload: {
					menu: payload
				}
			})
    },
    *showMenus({ payload }, { call, put }) {
			yield put({
				type: 'showMenusSuccess',
				payload: {
					menus: payload
				}
			})
		},
},

  reducers: {
    querySuccess1(state,action){
      const { successTotal } = action.payload
      return {
        ...state,
        successTotal
      }
    },
    borrowHistrySuccess(state,action){
      const { list1,pagination1 } = action.payload
      return {
        ...state,
        list1,
        pagination1
      }
    },
    borrowDetailsSuccess(state,action){
      const { list2,pagination2 } = action.payload
      return {
        ...state,
        list2,
        pagination2
      }
    },
    showModal(state, { payload }) {
      return {
        ...state,
        ...payload,
        modalVisibleApp: true,
        currentItem: payload.record
      };
    },
    hideModal(state) {
      return { ...state, modalVisibleApp: false };
    },
    stateAndType(state, { payload }) {
      return {
        ...state,
        operatingState: payload.operatingState,
        operatingType: payload.operatingType
      };
    },
    getSliderDetailSuccess(state, { payload }){
      return {
        ...state,
        currentItem: payload
      }
    },
    showCheckingModal(state, { payload }) {
      return {
        ...state,
        modalVisibleChecking: true,
        currentItem: payload
      };
    },
    hideCheckingModal(state, { payload }) {
      return { ...state, modalVisibleChecking: false };
    },
    showReturnModal(state, { payload }) {
      return {
        ...state,
        modalVisibleReturn: true,
        currentItem: payload.record
      };
    },
    hideReturnModal(state, { payload }) {
      return { ...state, modalVisibleReturn: false };
    },
    showDetailsSuccess(state, action) {
      const { showDetails, info } = action.payload;
      return {
        ...state,
        info,
        showDetails
      };
    },
    hidePageSuccess(state, action) {
      const { showDetails } = action.payload;
      return {
        ...state,
        showDetails
      };
    },
    borrowAgian(state,{ payload }){
      return {
        ...state,
        showBorrowModal:true,
        currentItem: payload
      }
    },
    hideBorrowAgain(state,{ payload }){
      return {
        ...state,
        showBorrowModal:false
      }
    },
    showAgainModal(state,{ payload }){
      return {
        ...state,
        showBorrowCheckModal:true,
        currentItem: payload
      }
    },
    hideAgainModal(state,{payload}){
      return {
        ...state,
        showBorrowCheckModal:false
      }
    },
    goList1(state,{payload}){
      return {
        ...state,
        goList:true,
      }
    },
    returnBack(state,{payload}){
      return {
        ...state,
        goList:false
      }
    },
    goDetails1(state,{payload}){
      return {
        ...state,
        goDetails:true
      }
    },
    returnList(state,{payload}){
      return {
        ...state,
        goDetails:false
      }
    },
    fistCom(state,{payload}){
      return{
        ...state,
        goList:false,
        goDetails:false,
      }
    },
    getSliderDetails(state,{payload}){
      return{
        ...state,
        sliderData:payload.record
      }
    },
    showMenuSuccess(state, action) {
			const { menu } = action.payload
			return {
				...state,
				menu
			}
    },
    showMenusSuccess(state,action){
      const { menus } = action.payload
      return {
        ...state,
        menus
      }
    },
    workflowSuccess(state,action){
      const { workflowList } = action.payload
      return{
        ...state,
        workflowList
      }
    }
  }
});
